延續著Day05的專案我們來一步一步了解maven的架構及使用
POM是Project Object Model的簡寫為專案物件模型,前一日提及maven是由Java撰寫的構建工具,所以既然要用程式來做事情,一定須要設定檔能夠讓我們進行各式各樣的task,讓我們攤開Day05的pom檔搭配官網的分類,逐步往下看
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
它定義了三個維度(groupId、artifactId、version)讓我們可以定位到專案的獨一無二性,就像是門牌地址一樣
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>6.2.11</version>
</dependency>
我們來看一個底下沒有分拆模組的esapi會是這樣定義它的artifactId
<!-- https://mvnrepository.com/artifact/org.owasp.esapi/esapi -->
<dependency>
<groupId>org.owasp.esapi</groupId>
<artifactId>esapi</artifactId>
<version>2.7.0.0</version>
</dependency>
我們在Day05的專案中沒有此標籤,預設的就是jar所以才未出現,如果是web的專案這邊可以改為war進行打包
這個部分就是放我們相依jar檔的地方,你只要給他maven座標(groupId、artifactId、version),它就會去預設的repoitory下載,就像我們在day05專案中寫入相關的套件
<dependencies>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
maven也提供了很多預先定義好的設定讓我們調用
<properties>
<springframework.version>6.2.11</springframework.version>
</properties>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- 略 -->
查看一下先前的例子,套件管理實際並不下載套件,僅是作為dependencies中的管理或是方便於管理子模組
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 由套件管理定義好相關的版本,我們在下方就不需要再加入版本號 -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<!-- Optionally: parameterized tests support -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
這邊定義了構建時所需要執行task的plugin訊息
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- 其他略 -->
</pluginManagement>
關於設置專案的相關訊息,例如Licenses、Organization、Developers、Contributors、專案名稱、專案網址等訊息,請參考官網設定方式